﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows;
using QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc;
using System.Data;

namespace QuanLyDuocPham.Controllers.QuanLyHeThong.NhapXuat
{
    class NHAP_CHITIET_Control : IDisposable
    {
        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
            if (ctrl1 != null)
            {
                ctrl1.Dispose();
                ctrl1 = null;
            }
        }
        public void loadComboBox(ComboBox cmb, string ma_phieunhap)
        {

            cmb.Items.Clear();
            var types = db.PHIEUNHAP_CT_GetByMaPhieunhap(ma_phieunhap);

            foreach (var t in types)
            {
                cmb.Items.Add(t);
            }
            
        }

        
        public void addData(string ma_nhap,  ListView ls, ComboBox cmbDuocPham, TextBox txtSo_Lo ,DatePicker dtpNgaySX, DatePicker dtpHan_Su_Dung, TextBox txtSo_Luong, TextBox txtGia_Ban, TextBox txtTL_ChietKhau, TextBox txtDien_Giai)
        {
            if (validateData( cmbDuocPham, txtSo_Lo, dtpHan_Su_Dung, txtSo_Luong, txtGia_Ban) == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {
                var cmb2Selected = (DUOCPHAM_Get_ListResult)cmbDuocPham.SelectedItem;

                db.PHIEUNHAP_CT_Insert(cmb2Selected.MA_DUOCPHAM,  ma_nhap, dtpNgaySX.SelectedDate,dtpHan_Su_Dung.SelectedDate, Int32.Parse(txtSo_Luong.Text), int.Parse(txtGia_Ban.Text), double.Parse(txtTL_ChietKhau.Text), txtDien_Giai.Text);
                MessageBox.Show("Đã thêm thành công");
                addTemp2(ls, cmbDuocPham, txtGia_Ban, txtSo_Luong);
            }
        }

        private bool validateData(ComboBox cmbDuocPham, TextBox txtSo_Lo, DatePicker dtpHan_Su_Dung, TextBox txtSo_Luong, TextBox txtGia_Ban) 
        {
            if (cmbDuocPham.SelectedItem == null || dtpHan_Su_Dung.SelectedDate == null ||  txtSo_Luong.Text.Trim() == "" || txtGia_Ban.Text.Trim() == "" || txtSo_Lo.Text.Trim() == "")
            {
                return false;
            }
            return true;
        }

        public void loadData(ListView List, string ma_kho, string ma_phieunhap)
        {
            List.Items.Clear();
            var list = db.PHIEUNHAP_CT_GetByMaPhieunhap( ma_phieunhap);

            List.ItemsSource = list;
        }

        
        DanhMuc.DUOCPHAM_Control ctrl1 = new DanhMuc.DUOCPHAM_Control();
        

        //---------------------------TEMP2---------------------------------------------
        public void clearTemp2()
        {
            foreach (var t in db.TEMP2_Get_List())
            {
                db.TEMP2_Delete(t.MA_DUOCPHAM);
            }
        }
        private void addTemp2(ListView ls, ComboBox cmbDuocPham, TextBox txtGia_Ban, TextBox txtSo_Luong)
        {
            var cmbDuocPhamSelected = (DUOCPHAM_Get_ListResult)cmbDuocPham.SelectedItem;
            db.TEMP2_Insert((ls.Items.Count + 1), cmbDuocPhamSelected.MA_DUOCPHAM, cmbDuocPhamSelected.TEN_DUOCPHAM, int.Parse(txtGia_Ban.Text), Int32.Parse(txtSo_Luong.Text), (int.Parse(txtGia_Ban.Text) * Int32.Parse(txtSo_Luong.Text)));
        }
        private void deleteTemp2(string ID1_Delete, ListView ls)
        {
            db.TEMP2_Delete(ID1_Delete);
            int stt = 1;
            foreach (var i in db.TEMP2_Get_List())
            {
                if (i.STT != stt)
                    i.STT = stt;
                stt++;
            }

        }

        private void updateTemp2(ListView ls, ComboBox cmbDuocPham, TextBox txtGia_Ban, TextBox txtSo_Luong)
        {
            var lsSelected = (TEMP2_Get_ListResult)ls.SelectedItem;
            var cmbDuocPhamSelected = (DUOCPHAM_Get_ListResult)cmbDuocPham.SelectedItem;
            db.TEMP2_Update(lsSelected.STT, cmbDuocPhamSelected.MA_DUOCPHAM, lsSelected.MA_DUOCPHAM, cmbDuocPhamSelected.TEN_DUOCPHAM, int.Parse(txtGia_Ban.Text), int.Parse(txtSo_Luong.Text), (int.Parse(txtGia_Ban.Text) * int.Parse(txtSo_Luong.Text)));
        }
        public void TongTien(Label lbTongTien)
        {
            decimal tong = 0;
            if (db.TEMP2_Get_List().Count() == 0)
            {
                lbTongTien.Content = "Tổng Thành Tiền = 0";
            }
            else
            {

                //var list = db.TEMP2_Get_List();
                //foreach (var i in list)
                //{
                //    tong = tong + decimal.Parse(i.THANH_TIEN.ToString());

                //}
                tong = decimal.Parse(db.TEMP2_Get_List().Sum(w => w.THANH_TIEN).ToString());
                lbTongTien.Content = "Tổng Thành Tiền = " + tong;
            }
        }
        public int TongTien(string ma_kho,string ma_nhap)
        {
            int tong = 0;
            if (db.PHIEUNHAP_CT_GetByMaPhieunhap(ma_nhap).Count() == 0)
            {
                tong = 0;
            }
            else
            {
                tong = int.Parse(db.PHIEUNHAP_CT_GetByMaPhieunhap( ma_nhap).Sum(w => (w.SO_LUONG * w.GIA_BAN)).ToString());

            }
            return tong;
        }
        public void loadDataTemp_Add(ListView List)
        {
            List.ItemsSource = null;
            var list = db.TEMP2_Get_List();
            foreach (var t in list)
            {
                List.Items.Add(t);
            }
        }

        GIA_Control giactrl = new GIA_Control();
        public void loadDataTemp2_Edit(string ma_nhap, ListView List)
        {
            var list = db.PHIEUNHAP_CT_GetByMaPhieunhap( ma_nhap);
            int stt = 1;
            foreach (var i in list)
            {
                db.TEMP2_Insert(stt, i.MA_DUOCPHAM, db.DUOCPHAM_GetByMaDuocpham(i.MA_DUOCPHAM).Single().TEN_DUOCPHAM, int.Parse(i.GIA_BAN.ToString()), i.SO_LUONG, (int.Parse(i.GIA_BAN.ToString()) * i.SO_LUONG));
                    stt++;
            }
            List.ItemsSource = db.TEMP2_Get_List();
        }


        public void addandloadDataTemp2()
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                int stt = 1;
                foreach (DataRow i in ds.Tables[0].Rows)
                {
                    db.TEMP2_Insert(stt, i.ItemArray[0].ToString(), db.DUOCPHAM_GetByMaDuocpham(i.ItemArray[0].ToString()).Single().TEN_DUOCPHAM, int.Parse(i.ItemArray[5].ToString()), int.Parse(i.ItemArray[4].ToString()), (int.Parse(i.ItemArray[5].ToString()) * int.Parse(i.ItemArray[4].ToString())));
                    stt++;
                }
            }
 
        }
        
        //-------------------------Xử lý trên bảng tạm------------------
        DataSet ds = new DataSet();

        public void TaoBangTam()
        {
            
            var names = db.Mapping.MappingSource.GetModel(typeof(DuocPhamDataContext)).GetMetaType(typeof(PHIEUNHAP_CT)).DataMembers;
            foreach (var i in names)
            {
                ds.Tables[0].Columns.Add( i.MappedName, i.Type);
            }
        }
        public void XuLyBangTam(string thaotac, string ma_nhap, ListView lv1, ComboBox cmbDuocPham, DatePicker dtpNgay_San_Xuat, DatePicker dtpHan_Su_Dung, TextBox txtSoLuong, TextBox txtGia_Ban, TextBox txtTL_ChietKhau, RichTextBox txtDien_Giai)
        {
            var cmbDuocPhamSelected = (DUOCPHAM_Get_ListResult)cmbDuocPham.SelectedItem;
            switch (thaotac)
            {

                case "add":
                    DataRow dr;
                    
                    dr = ds.Tables[0].NewRow();
                    dr[0] = cmbDuocPhamSelected.MA_DUOCPHAM;
                    
                    dr[1] = ma_nhap;
                    dr[2] = dtpNgay_San_Xuat.SelectedDate;
                    dr[3] = dtpHan_Su_Dung.SelectedDate;
                    dr[4] = int.Parse( txtSoLuong.Text);
                    dr[5] = int.Parse(txtGia_Ban.Text);
                    dr[6] = double.Parse(txtTL_ChietKhau.Text);
                    dr[7] = txtDien_Giai.Document;

                    break;


                case "update":
                    
                    ds.Tables[0].Rows[lv1.SelectedIndex].ItemArray[0] = cmbDuocPhamSelected.MA_DUOCPHAM;
                    
                    ds.Tables[0].Rows[lv1.SelectedIndex].ItemArray[1] = ma_nhap;
                    ds.Tables[0].Rows[lv1.SelectedIndex].ItemArray[2] = dtpNgay_San_Xuat.SelectedDate;
                    ds.Tables[0].Rows[lv1.SelectedIndex].ItemArray[3] = dtpHan_Su_Dung.SelectedDate;
                    ds.Tables[0].Rows[lv1.SelectedIndex].ItemArray[4] = int.Parse(txtSoLuong.Text);
                    ds.Tables[0].Rows[lv1.SelectedIndex].ItemArray[5] = int.Parse(txtGia_Ban.Text);
                    ds.Tables[0].Rows[lv1.SelectedIndex].ItemArray[6] = double.Parse(txtTL_ChietKhau.Text);
                    ds.Tables[0].Rows[lv1.SelectedIndex].ItemArray[7] = txtDien_Giai.Document;
                    

                    break;


                case "delete":
                    var lvselect = (TEMP2_Get_ListResult)lv1.SelectedItem;
                    ds.Tables[0].Rows.RemoveAt(lv1.SelectedIndex);
                   

                    break;
            }

        }
        public void XoaBangTam()
        {
            ds.Dispose();
        }

        public void NHAPCTToTam(string ma_nhap)
        {
            if (db.PHIEUNHAP_CT_GetByMaPhieunhap( ma_nhap).Count() > 0)
            {
                ds.Dispose();
                foreach (var i in db.PHIEUNHAP_CT_GetByMaPhieunhap( ma_nhap))
                {
                    DataRow dr;

                    dr = ds.Tables[0].NewRow();
                    dr[0] = i.MA_DUOCPHAM;
                    
                    dr[1] = ma_nhap;
                    dr[2] = i.NGAY_SAN_XUAT;
                    dr[3] = i.HAN_SU_DUNG;
                    dr[4] = i.SO_LUONG;
                    dr[5] = i.GIA_BAN;
                    dr[6] = i.TL_CHIETKHAU;
                    dr[7] = i.DIEN_GIAI;
                    ds.Tables[0].Rows.Add(dr);
                }
            }
        }

        public void TamToView(string ma_nhap, ListView lv1, ComboBox cmbDuocPham, DatePicker dtpNgay_San_Xuat, DatePicker dtpHan_Su_Dung, TextBox txtSoLuong, TextBox txtGia_Ban, TextBox txtTL_ChietKhau, RichTextBox txtDien_Giai)
        {

            var sl = ds.Tables[0].Rows[lv1.SelectedIndex];
            ctrl1.loadComboBox(cmbDuocPham);
            int t = 0;
            foreach (var i in cmbDuocPham.Items)
            {
                var temp = (DUOCPHAM_Get_ListResult)i;
                if (temp.MA_DUOCPHAM == sl.ItemArray[1].ToString())
                {
                    break;
                }
                t++;
            }
            cmbDuocPham.SelectedIndex = t;
           
            
            dtpNgay_San_Xuat.SelectedDate = DateTime.Parse( sl.ItemArray[3].ToString());
            dtpHan_Su_Dung.SelectedDate = DateTime.Parse(sl.ItemArray[4].ToString());
            txtSoLuong.Text=  sl.ItemArray[5].ToString();
             txtGia_Ban.Text = sl.ItemArray[6].ToString();
             txtTL_ChietKhau.Text = sl.ItemArray[7].ToString();
             txtDien_Giai.AppendText( sl.ItemArray[8].ToString());



        }
        //--------------------------------------------------------------
        public void TamToNHAPCT()
        {
            foreach (DataRow  i in ds.Tables[0].Rows)
            {
                db.PHIEUNHAP_CT_Insert(i.ItemArray[0].ToString(), i.ItemArray[1].ToString(), DateTime.Parse(i.ItemArray[2].ToString()), DateTime.Parse( i.ItemArray[3].ToString()), int.Parse( i.ItemArray[4].ToString()), int.Parse( i.ItemArray[5].ToString()), double.Parse( i.ItemArray[6].ToString()), i.ItemArray[7].ToString());
            }
        }

        public void deleteAllNhapCT( string ma_nhap)
        {
            if (db.PHIEUNHAP_CT_GetByMaPhieunhap( ma_nhap).Count() > 0)
            {
                foreach (var i in db.PHIEUNHAP_CT_GetByMaPhieunhap( ma_nhap))
                {
                    db.PHIEUNHAP_CT_Delete(i.MA_DUOCPHAM, ma_nhap);
                }
            }
        }
    }
}
